package com.bw.contorller;

import com.bw.entity.User;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Action;

@Controller
public class UserContorller {
    @Autowired
    UserService userService;

    @RequestMapping("tologin")
    public String toLogin(User user, Model model){
        model.addAttribute("user",user);
        return "login";
    }
    @RequestMapping("login")
    public String Login(User user, Model model, HttpSession session){
        System.out.println(user);
        Integer count = (Integer) session.getAttribute(user.getName());
        if(count==null){
            count=1;
        }else{
            count++;
        }
        session.setAttribute(user.getName(),count);
        session.setAttribute("count",count);
        User user1=userService.selectByName(user);
        if(user1!=null){
            session.setAttribute("user",user1);
            return "redirect:toindex";
        }
        return "login";
    }
}
